﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Converters;
using System.Windows.Media.Composition;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using GadgetBase;


namespace CalendarGadget
{
    public class CalendarGadget : GadgetWindow
    {
        private Label year = new Label();
        private Label day = new Label();
        private Label dayOfWeek = new Label();

        private DispatcherTimer timer = new DispatcherTimer();

        public CalendarGadget()
            : base()
        {
            this.Title = "Calendar";

            base.Width = 130;
            base.Height = 141;

            Calendar calendar = new Calendar();
            base.Children.Add(calendar);
            
            year.HorizontalAlignment = HorizontalAlignment.Center;
            year.VerticalAlignment = VerticalAlignment.Top;
            year.HorizontalContentAlignment = HorizontalAlignment.Center;
            year.Foreground = Brushes.White;
            year.FontSize = 13;
            year.Margin = new Thickness(0, 12, 0, 0);
            
            day.HorizontalAlignment = HorizontalAlignment.Center;
            day.VerticalAlignment = VerticalAlignment.Center;
            day.Foreground = Brushes.White;
            day.FontSize = 75;
            
            dayOfWeek.HorizontalAlignment = HorizontalAlignment.Center;
            dayOfWeek.VerticalAlignment = VerticalAlignment.Bottom;
            dayOfWeek.Foreground = Brushes.White;
            dayOfWeek.FontSize = 13;
            dayOfWeek.Margin = new Thickness(0, 0, 0, 5);

            RefreshTime();

            base.Children.Add(year);
            base.Children.Add(day);
            base.Children.Add(dayOfWeek);            
        }

        private void RefreshTime()
        {
            if (DateTime.Now.Month == 5 && string.Equals(CultureInfo.CurrentCulture.TwoLetterISOLanguageName, "be", StringComparison.OrdinalIgnoreCase))
            {
                year.Content = "Травень " + DateTime.Now.Year;
            }
            else
            {
                year.Content = DateTime.Now.ToString("Y");
            }
            day.Content = DateTime.Now.Day;
            dayOfWeek.Content = DateTime.Now.ToString("dddd");

            DateTime next = DateTime.Today.AddDays(1);
            timer.Interval = next - DateTime.Now;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            RefreshTime();
        }


        public class Calendar : GadgetBase.Gadgets.Gadget
        {
            private string folder;

            private const string backgroundImageName = "calendar_single_orange.png";            

            public Calendar()
            {
                this.folder = GlobalMethods.GetFullPath(@"images\");

                SetBackground(folder + backgroundImageName);                                
            }                                   
        }
    }
}
